<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form name="loginForm">
        <div>
            <label for="phoneTnput">手机</label>
            <input type="text" name="phone" id="phoneTnput" placeholder="请输入手机" required>
        </div>
        <div>
            <button type="submit">提交</button>
        </div>
    </form>
    <script>
        const oForm = document.forms['loginForm'];
        // console.log(oForm);
        oForm.addEventListener('submit', function(event) {
            // console.log(event);
            event.preventDefault();
            const phone = oForm['phone'].value.trim();
            console.log(phone);
            if (!phone.length) {
                console.log('没写入东西');
            }
            // console.log(this);
            // this.submit();
            if (!/^[0-9]{11}$/.test(phone)) {
                alert('手机格式有误');
                return
            }
        })
    </script>
</body>

</html>